﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataLayer;

namespace Manager
{
    public partial class frmImports : XtraForm
    {
        public frmImports()
        {
            InitializeComponent();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";
            dlg.Title = "Stok Aktarmak İçin Uygun Exel Dosyası Seçin";
            dlg.Filter = "Excel Dosyası|*.xls";
            c1FlexGrid1.Rows.Count = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                List<string> col = new List<string>();
                Object fnt = c1FlexGrid1.Font.Clone();

                for (int i = 0; i < c1FlexGrid1.Cols.Count; i++)
                    col.Add(c1FlexGrid1.Cols[i].Caption);
                c1FlexGrid1.LoadExcel(dlg.FileName);
                for (int i = 0; i < col.Count; i++)
                {
                    c1FlexGrid1.Cols[i].Caption = col[i];
                }
                c1FlexGrid1.Font = fnt as Font;
                c1FlexGrid1.AutoSizeCols();
            }
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.FileName = "";
            dlg.Title = "Cari Aktarmak İçin Uygun Exel Dosyası Seçin";
            dlg.Filter = "Excel Dosyası|*.xls";
            c1FlexGrid1.Rows.Count = 1;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                List<string> col = new List<string>();
                Object fnt = c1FlexGrid2.Font.Clone();

                for (int i = 0; i < c1FlexGrid2.Cols.Count; i++)
                    col.Add(c1FlexGrid2.Cols[i].Caption);
                c1FlexGrid2.LoadExcel(dlg.FileName);
                for (int i = 0; i < col.Count; i++)
                {
                    c1FlexGrid2.Cols[i].Caption = col[i];
                }
                c1FlexGrid2.Font = fnt as Font;
                c1FlexGrid2.AutoSizeCols();
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (c1FlexGrid1.Rows.Count > 1) //stok kayıt
            {
                IDbDataParameter barkod = Factory.GetFactory.GetParameter("@barkod", 25, DbType.String);
                IDbDataParameter stokkod = Factory.GetFactory.GetParameter("@stokkod", 25, DbType.String);
                IDbDataParameter stokad = Factory.GetFactory.GetParameter("@stokad", 50, DbType.String);
                IDbDataParameter doviz = Factory.GetFactory.GetParameter("@doviz", DbType.Int32);
                IDbDataParameter fiyat = Factory.GetFactory.GetParameter("@fiyat", DbType.Decimal);
                IDbDataParameter kdv = Factory.GetFactory.GetParameter("@kdv", DbType.Int32);
                IDbDataParameter saglayici = Factory.GetFactory.GetParameter("@saglayici", 25, DbType.String);
                IDbDataParameter isk1 = Factory.GetFactory.GetParameter("@isk1", DbType.Decimal);
                IDbDataParameter isk2 = Factory.GetFactory.GetParameter("@isk2", DbType.Decimal);
                IDbDataParameter isk3 = Factory.GetFactory.GetParameter("@isk3", DbType.Decimal);
                IDbDataParameter birim = Factory.GetFactory.GetParameter("@birim", 25, DbType.String);

                IDbDataParameter marka = Factory.GetFactory.GetParameter("@brand_code", 25, DbType.String);
                IDbDataParameter ithalat = Factory.GetFactory.GetParameter("@import_code", 25, DbType.String);
                IDbDataParameter ihracat = Factory.GetFactory.GetParameter("@export_code", 25, DbType.String);
                IDbDataParameter maliyet = Factory.GetFactory.GetParameter("@cost_code", 25, DbType.String);
                IDbDataParameter ozel1 = Factory.GetFactory.GetParameter("@custom_code1", 50, DbType.String);
                IDbDataParameter ozel2 = Factory.GetFactory.GetParameter("@custom_code2", 50, DbType.String);

                string sqltxt = "if exists(SELECT barcode FROM PRODUCTS WITH (NOLOCK) WHERE (barcode = @barkod)) update PRODUCTS set code=@stokkod, name=@stokad, currency=@doviz, price=@fiyat, vat=@kdv, supplier=@saglayici, new=0,discount1=@isk1,discount2=@isk2,discount3=@isk3,unit=@birim,brand_code=@brand_code,import_code=@import_code,export_code=@export_code,cost_code=@cost_code,custom_code1=@custom_code1,custom_code2=@custom_code2 WHERE (barcode = @barkod) else insert into PRODUCTS (barcode, code, name, currency, price, vat, supplier, new,discount1,discount2,discount3,unit,brand_code,import_code,export_code,cost_code,custom_code1,custom_code2) values (@barkod,@stokkod,@stokad,@doviz,@fiyat,@kdv,@saglayici,0,@isk1,@isk2,@isk3,@birim,@brand_code,@import_code,@export_code,@cost_code,@custom_code1,@custom_code2)";

                for (int i = 1; i < c1FlexGrid1.Rows.Count; i++)
                {
                    if (c1FlexGrid1[i, 0] != null && c1FlexGrid1[i, 0] != null)
                    {
                        if (c1FlexGrid1[i, 0] != null) barkod.Value = c1FlexGrid1[i, 0].ToString(); else barkod.Value = "";
                        if (c1FlexGrid1[i, 1] != null) stokkod.Value = c1FlexGrid1[i, 1].ToString(); else stokkod.Value = "";
                        if (c1FlexGrid1[i, 2] != null) stokad.Value = c1FlexGrid1[i, 2].ToString(); else stokad.Value = "";
                        if (c1FlexGrid1[i, 3] != null) doviz.Value = Convert.ToInt32(c1FlexGrid1[i, 3]); else doviz.Value = 0;
                        if (c1FlexGrid1[i, 4] != null) fiyat.Value = Convert.ToDecimal(c1FlexGrid1[i, 4]); else fiyat.Value = 0;
                        if (c1FlexGrid1[i, 5] != null) kdv.Value = Convert.ToInt32(c1FlexGrid1[i, 5]); else kdv.Value = 18;
                        if (c1FlexGrid1[i, 6] != null) saglayici.Value = c1FlexGrid1[i, 6].ToString(); else saglayici.Value = "";
                        if (c1FlexGrid1[i, 7] != null) isk1.Value = c1FlexGrid1[i, 7].ToString(); else isk1.Value = 0;
                        if (c1FlexGrid1[i, 8] != null) isk2.Value = c1FlexGrid1[i, 8].ToString(); else isk2.Value = 0;
                        if (c1FlexGrid1[i, 9] != null) isk3.Value = c1FlexGrid1[i, 9].ToString(); else isk3.Value = 0;
                        if (c1FlexGrid1[i, 10] != null) birim.Value = c1FlexGrid1[i, 10].ToString(); else birim.Value = "";

                        if (c1FlexGrid1[i, 11] != null) marka.Value = c1FlexGrid1[i, 11].ToString(); else marka.Value = "";
                        if (c1FlexGrid1[i, 12] != null) ithalat.Value = c1FlexGrid1[i, 12].ToString(); else ithalat.Value = "";
                        if (c1FlexGrid1[i, 13] != null) ihracat.Value = c1FlexGrid1[i, 13].ToString(); else ihracat.Value = "";
                        if (c1FlexGrid1[i, 14] != null) maliyet.Value = c1FlexGrid1[i, 14].ToString(); else maliyet.Value = "";
                        if (c1FlexGrid1[i, 15] != null) ozel1.Value = c1FlexGrid1[i, 15].ToString(); else ozel1.Value = "";
                        if (c1FlexGrid1[i, 16] != null) ozel2.Value = c1FlexGrid1[i, 16].ToString(); else ozel2.Value = "";

                        if (!string.IsNullOrEmpty(c1FlexGrid1[i, 0].ToString()) && !string.IsNullOrEmpty(c1FlexGrid1[i, 1].ToString()) && !string.IsNullOrEmpty(c1FlexGrid1[i, 2].ToString()) && Convert.ToDecimal(fiyat.Value) > 0)
                            Factory.GetFactory.ExecuteNonQuery(CommandType.Text, sqltxt, barkod, stokkod, stokad, doviz, fiyat, kdv, saglayici, isk1, isk2, isk3, birim, marka, ithalat, ihracat, maliyet, ozel1, ozel2);
                        else
                            MessageBox.Show(c1FlexGrid1[i, 2].ToString() + " -Ürününün Barkod,Kodu,Adı ve Fiyat Bilgileri Düzgün Girilmemiş");
                    }
                }
            }

            if (c1FlexGrid2.Rows.Count > 1) //cari kayıt
            {

                IDbDataParameter code = Factory.GetFactory.GetParameter("@code", 25, DbType.String);
                IDbDataParameter name = Factory.GetFactory.GetParameter("@name", 50, DbType.String);
                IDbDataParameter taxoffice = Factory.GetFactory.GetParameter("@taxoffice", 50, DbType.String);
                IDbDataParameter taxnumber = Factory.GetFactory.GetParameter("@taxnumber", 25, DbType.String);
                IDbDataParameter telnumber = Factory.GetFactory.GetParameter("@telnumber", 25, DbType.String);
                IDbDataParameter faxnumber = Factory.GetFactory.GetParameter("@faxnumber", 25, DbType.String);
                IDbDataParameter adress = Factory.GetFactory.GetParameter("@adress", 200, DbType.String);
                IDbDataParameter order_series = Factory.GetFactory.GetParameter("@order_series", 5, DbType.String);
                IDbDataParameter limit1 = Factory.GetFactory.GetParameter("@limit1", DbType.Decimal);
                IDbDataParameter limit2 = Factory.GetFactory.GetParameter("@limit2", DbType.Decimal);
                IDbDataParameter bolge = Factory.GetFactory.GetParameter("@bolge", DbType.String);

                IDbDataParameter plasiyer = Factory.GetFactory.GetParameter("@seller_person", 50, DbType.String);
                IDbDataParameter sehir = Factory.GetFactory.GetParameter("@city", 50, DbType.String);
                IDbDataParameter ilce = Factory.GetFactory.GetParameter("@district", 50, DbType.String);
                IDbDataParameter ozel1 = Factory.GetFactory.GetParameter("@custom_code1", 50, DbType.String);
                IDbDataParameter ozel2 = Factory.GetFactory.GetParameter("@custom_code2", 50, DbType.String);

                string sqltxt = "if exists(SELECT code FROM CUSTOMERS WITH (NOLOCK) WHERE (code = @code)) update CUSTOMERS set  name=@name, taxoffice=@taxoffice, taxnumber=@taxnumber, telnumber=@telnumber, faxnumber=@faxnumber, adress=@adress, order_series=@order_series,including_vat_limit=@limit1,excluding_vat_limit=@limit2,area_code=@bolge,seller_person=@seller_person,city=@city,district=@district,custom_code1=@custom_code1,custom_code2=@custom_code2 where code = @code else insert into CUSTOMERS (code, name, taxoffice, taxnumber, telnumber, faxnumber, adress, order_series,including_vat_limit,excluding_vat_limit,area_code,seller_person,city,district,custom_code1,custom_code2) values (@code, @name, @taxoffice, @taxnumber, @telnumber, @faxnumber, @adress, @order_series,@limit1,@limit2,@bolge,@seller_person,@city,@district,@custom_code1,@custom_code2)";

                for (int i = 1; i < c1FlexGrid2.Rows.Count; i++)
                {
                    if (c1FlexGrid2[i, 0] != null && c1FlexGrid2[i, 0] != null)
                    {
                        if (c1FlexGrid2[i, 0] != null) code.Value = c1FlexGrid2[i, 0].ToString(); else code.Value = "";
                        if (c1FlexGrid2[i, 1] != null) name.Value = c1FlexGrid2[i, 1].ToString(); else name.Value = "";
                        if (c1FlexGrid2[i, 2] != null) taxoffice.Value = c1FlexGrid2[i, 2].ToString(); else taxoffice.Value = "";
                        if (c1FlexGrid2[i, 3] != null) taxnumber.Value = c1FlexGrid2[i, 3].ToString(); else taxnumber.Value = "";
                        if (c1FlexGrid2[i, 4] != null) telnumber.Value = c1FlexGrid2[i, 4].ToString(); else telnumber.Value = "";
                        if (c1FlexGrid2[i, 5] != null) faxnumber.Value = c1FlexGrid2[i, 5].ToString(); else faxnumber.Value = "";
                        if (c1FlexGrid2[i, 6] != null) adress.Value = c1FlexGrid2[i, 6].ToString(); else adress.Value = "";
                        if (c1FlexGrid2[i, 7] != null) order_series.Value = c1FlexGrid2[i, 7].ToString(); else order_series.Value = "";

                        if (c1FlexGrid2[i, 8] != null) limit1.Value = Convert.ToDecimal(c1FlexGrid2[i, 8]); else limit1.Value = 0;
                        if (c1FlexGrid2[i, 9] != null) limit2.Value = Convert.ToDecimal(c1FlexGrid2[i, 9]); else limit2.Value = 0;
                        if (c1FlexGrid2[i, 10] != null) bolge.Value = c1FlexGrid2[i, 10].ToString(); else bolge.Value = "";

                        if (c1FlexGrid2[i, 11] != null) plasiyer.Value = c1FlexGrid2[i, 11].ToString(); else plasiyer.Value = "";
                        if (c1FlexGrid2[i, 12] != null) sehir.Value = c1FlexGrid2[i, 12].ToString(); else sehir.Value = "";
                        if (c1FlexGrid2[i, 13] != null) ilce.Value = c1FlexGrid2[i, 13].ToString(); else ilce.Value = "";
                        if (c1FlexGrid2[i, 14] != null) ozel1.Value = c1FlexGrid2[i, 14].ToString(); else ozel1.Value = "";
                        if (c1FlexGrid2[i, 15] != null) ozel2.Value = c1FlexGrid2[i, 15].ToString(); else ozel2.Value = "";


                        if (c1FlexGrid2[i, 0] != null && c1FlexGrid2[i, 7] != null)
                            Factory.GetFactory.ExecuteNonQuery(CommandType.Text, sqltxt, code, name, taxoffice, taxnumber, telnumber, faxnumber, adress, order_series, limit1, limit2, bolge, plasiyer, sehir, ilce, ozel1, ozel2);
                    }
                }
            }
            MessageBox.Show("Aktarım İşlemi Tamamlandı !");
        }
    }
}
